package org.zxw.cms.service.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.zxw.basic.model.Pager;
import org.zxw.cms.dao.IGroupDao;
import org.zxw.cms.dao.IRoleDao;
import org.zxw.cms.dao.IUserDao;
import org.zxw.cms.model.CmsException;
import org.zxw.cms.model.Group;
import org.zxw.cms.model.Role;
import org.zxw.cms.model.User;
import org.zxw.cms.service.IUserService;

public class UserServiceImpl implements IUserService {
	private IUserDao userDao;
	private IRoleDao roleDao;
	private IGroupDao groupDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	@Inject
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	@Inject
	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IGroupDao getGroupDao() {
		return groupDao;
	}

	@Inject
	public void setGroupDao(IGroupDao groupDao) {
		this.groupDao = groupDao;
	}

	@Override
	public void add(User user, Integer[] roleIds, Integer[] groupIds) {
		User u = userDao.loadByUsername(user.getUsername());
		if (u != null) {
			throw new CmsException("要添加的用户已经存在");
		}
		user.setCreateDate(new Date());
		userDao.add(user);
		for (Integer roleId : roleIds) {
			addUserRole(user, roleId);
		}
		for (Integer groupId : groupIds) {
			addUserGroup(user, groupId);
		}
	}

	private void addUserGroup(User user, Integer groupId) {
		Group group = groupDao.load(groupId);
		if (group == null) {
			throw new CmsException("要添加用户的组不存在");
		}
		userDao.addUserGroup(user, group);
	}

	private void addUserRole(User user, Integer roleId) {
		Role role = roleDao.load(roleId);
		if (role == null) {
			throw new CmsException("要添加用户的角色不存在");
		}
		userDao.addUserRole(user, role);
	}

	@Override
	public void delete(int userId) {
		userDao.deleteUserGroups(userId);
		userDao.deleteUserRoles(userId);
		userDao.delete(userId);
	}

	@Override
	public void update(User user, Integer[] roleIds, Integer[] groupIds) {

	}

	@Override
	public void updateStatus(int userId) {
		User user = userDao.load(userId);
		if (user == null) {
			throw new CmsException("要修改的用户不存在");
		}
		if (user.getStatus() == 0) {
			user.setStatus(1);
		} else {
			user.setStatus(0);
		}
	}

	@Override
	public Pager<User> findUser() {
		return userDao.findUser();
	}

	@Override
	public User load(int userId) {
		return userDao.load(userId);
	}

	@Override
	public List<Role> listUserRoles(int userId) {
		return userDao.listUserRoles(userId);
	}

	@Override
	public List<Group> listUserGroups(int userId) {
		return userDao.listUserGroups(userId);
	}

	@Override
	public List<Integer> listUserRoleIds(int userId) {
		return userDao.listUserRoleIds(userId);
	}

	@Override
	public List<Integer> listUserGroupIds(int userId) {
		return userDao.listUserGroupIds(userId);
	}

	@Override
	public List<User> listGroupUsers(int groupId) {
		return userDao.listGroupUsers(groupId);
	}

	@Override
	public List<User> listRoleUsers(int roleId) {
		return userDao.listRoleUsers(roleId);
	}

}
